import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:full_display/common/qxp_color.dart';
import 'package:full_display/common/qxp_style.dart';
import 'package:full_display/unit/screen/screen_utility.dart';

class EmailRegister extends StatefulWidget {

  final String codeStr;
  final Function callBack;
  final Function registerEmail;
  const EmailRegister({Key? key, required this.codeStr, required this.callBack, required this.registerEmail}) : super(key: key);

  @override
  State<EmailRegister> createState() => _EmailRegisterState();
}

class _EmailRegisterState extends State<EmailRegister> {
  final TextEditingController _emailController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();
  final TextEditingController _passwordConfigController = TextEditingController();
  final TextEditingController _codeEmailController = TextEditingController();

  final map = {'Email': '', 'password': '', 'passwordConfig': '', 'verificationCode': ''};

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Expanded(
                child: TextField(
                  onChanged: (text){
                    _handleInput('Email', text);
                  },
                  maxLines: 1,
                  inputFormatters: [
                      FilteringTextInputFormatter.allow(RegExp(r'[a-zA-Z0-9@.]')),
                  ],
                  autofocus: true,
              controller: _emailController,
              style: StandardTextStyle.inputHintStyle,
              decoration: InputDecoration(
                  hintText: '请输入邮箱',
                  hintStyle: StandardTextStyle.inputHintStyle,
                  border: InputBorder.none),
            ))
          ],
        ),
        Divider(
          height: 0.5.H,
          color: ColorPlate.white.withOpacity(0.15),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
                child: SizedBox(
              height: 67.0.H,
              child: TextField(
                onChanged: (text){
                  _handleInput('password', text);
                },
                maxLines: 1,
                inputFormatters: [FilteringTextInputFormatter.allow(RegExp(r'[a-zA-Z0-9\W]'))],
                controller: _passwordController,
                style: StandardTextStyle.inputHintStyle,
                decoration: InputDecoration(
                    hintText: '请输入密码',
                    hintStyle: StandardTextStyle.inputHintStyle,
                    border: InputBorder.none),
              ),
            )),
          ],
        ),
        Divider(
          height: 0.5.H,
          color: ColorPlate.white.withOpacity(0.15),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
                child: SizedBox(
              height: 67.0.H,
              child: TextField(
                onChanged: (text){
                  _handleInput('passwordConfig', text);
                },
                maxLines: 1,
                inputFormatters: [FilteringTextInputFormatter.allow(RegExp(r'[a-zA-Z0-9\W]'))],
                controller: _passwordConfigController,
                style: StandardTextStyle.inputHintStyle,
                decoration: InputDecoration(
                    hintText: '请再次输入密码',
                    hintStyle: StandardTextStyle.inputHintStyle,
                    border: InputBorder.none),
              ),
            )),
          ],
        ),
        Divider(
          height: 0.5.H,
          color: ColorPlate.white.withOpacity(0.15),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
                child: SizedBox(
              height: 67.0.H,
              child: TextField(
                maxLines: 1,
                maxLength: 6,
                keyboardType: TextInputType.number,
                inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly],
                controller: _codeEmailController,
                style: StandardTextStyle.inputHintStyle,
                decoration: InputDecoration(
                    hintText: '请输入验证码',
                    counterText: '',
                    hintStyle: StandardTextStyle.inputHintStyle,
                    border: InputBorder.none),
              ),
            )),
            GestureDetector(
              onTap: () {
                widget.registerEmail.call();
              },
              child: Text(
                widget.codeStr,
                style: StandardTextStyle.textBlue16Style,
              ),
            )
          ],
        ),
        Divider(
          height: 0.5.H,
          color: ColorPlate.white.withOpacity(0.15),
        ),
      ],
    );
  }

  _handleInput(String type, String text) {
    switch (type) {
      case 'Email':
        map['Email'] = text;
        break;
      case 'password':
        map['password'] = text;
        break;
      case 'passwordConfig':
        map['passwordConfig'] = text;
        break;
      case 'verificationCode':
        map['verificationCode'] = text;
        break;
    }
    widget.callBack.call(map);
  }
}
